//LOADER.CPP --- Absolute loader for SIC machine
//This program accepts program with .obj extension

#include<fstream.h>
#include<conio.h>
#include<string.h>
#include"hexdec.cpp"

class objinmem
{
 private:
  char objcode[3];
  char addr[5];
 public:
  void setcode(char *x) {  strcpy(objcode,x); }
  void setaddr(char *x) {  strcpy(addr,x); }
  char * retaddr() 	{  return addr; }
  void outaddr() 	{  cout<<addr; }
  void outcode() 	{  cout<<objcode;}
};

void main()
{
 objinmem objclass[200];
 char fname[30],str[80],instr[10];
 char name[10],flength[10],reclen[3];
 char staddr[12][7],recsize[10][7],temp[5];
 int count=0,i,j,k,lenrec,cnt=0,cnt1=0;
 int flag=0,n;

 clrscr();
 cout<<"\n Enter File Name: ";
 cin>>fname;

 ifstream infile(fname);
 if(!infile)
  {cout<<" Error in opening file"; exit(0); }
 else
 {
   while(!infile.eof() && flag==0)
   { infile.getline(str,80);
	 if(str[0]=='H')        // header record
	  {for(i=1;i<=6;i++)
	    name[i-1]=str[i];	  name[6]='\0';
	   for(i=13;i<=18;i++)
	    flength[i-13]=str[i];  flength[i]='\0';
	  }
	 else if(str[0]=='E')   // end record
	  { flag=1;}
	 else
	  {
	   for(i=3;i<=6;i++)
	    {  temp[i-3]=str[i]; }  temp[4]='\0';
	   strcpy(staddr[cnt1++],temp);
	   reclen[0]=str[7];	  reclen[1]=str[8];	  reclen[2]='\0';
	   strcpy(recsize[cnt++],reclen);
	   lenrec=0;
	   hex(reclen,'+',lenrec);

	   for(i=1;i<=lenrec;i++)
	    {
	     k=0;
	     for(j=i*2+7;j<=i*2+8;j++)
	      { instr[k]=str[j]; k++;} instr[k]='\0';
	     objclass[count].setcode(instr);
	     objclass[count].setaddr(temp);
	     n=1; hex(temp,'+',n);
	     count++;
	  }
	 }//if-else
   }//while
 }//main if-else

 cout<<"\n\n Name of source file: "<<name;
 cout<<"\n Program length     : "<<flength<<"\n\n";

 j=0;
 for(i=0;i<count;i++)
  {
   objclass[i].outaddr();  cout<<" ";
   objclass[i].outcode();  cout<<"\t";  j++;
   if(wherey()==20)   { getch(); clrscr(); j=0; };
  }
 getch();
}